import { queryCompanies } from '@/pages/SystemUser/service';
import { specialCharReg, urlReg } from '@/utils/utils';
import {
  ProForm,
  ModalForm,
  ProFormSelect,
  ProFormSwitch,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { message } from 'antd';
import { updatePkg } from '../service';
import { PackageStatusEnum, PackageTableItem, PackageTypeEnum, UpdatePkgParams } from '../types.d';

const UpdatePkg = (props: {
  close: () => void;
  reload: () => void;
  isAdmin: boolean;
  currItem: PackageTableItem;
  visible: boolean;
}) => {
  const { close, isAdmin, currItem, visible, reload } = props;
  return (
    <ModalForm<UpdatePkgParams>
      layout="vertical"
      title="更新包"
      visible={visible}
      modalProps={{
        destroyOnClose: true,
        maskClosable: false,
        onCancel: () => {
          close();
        },
      }}
      initialValues={currItem}
      onFinish={async (values) => {
        const res = await updatePkg(currItem._id, values);
        if (res.success) {
          message.success({
            content: res.message,
            key: 'update_pkg',
          });
          reload();
          close();
          return true;
        }
        message.error({ content: res.message, key: 'update_pkg' });
        return false;
      }}
    >
      {isAdmin && (
        <ProFormSelect
          width="xl"
          name="ownerId"
          label="归属公司"
          debounceTime={300}
          showSearch
          placeholder="请选择归属公司(如未找到目标公司，请优先创建[企业管理者])"
          request={(val) => queryCompanies(val.keyWords)}
        />
      )}
      <ProFormText
        name="pkName"
        label="包名称"
        width="xl"
        placeholder="请输入当前包名称(请确保当前包名唯一性, 且不支持特殊字符)"
        rules={[
          {
            required: true,
            validator: (_, val) => {
              if (!val || specialCharReg.test(val))
                return Promise.reject(new Error('包名不符合规范，不支持包含特殊字符'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProForm.Group>
        <ProFormText
          name="version"
          label="版本号"
          width="md"
          placeholder="当前包的版本号, 例如1.0.0、1.1.0-alpha"
          rules={[
            {
              required: true,
              validator: (_, val) => {
                if (!val || !/^\d+\.\d+.[\w_-]+$/.test(val))
                  return Promise.reject(new Error('版本号不符合规范，请按照x.x.x格式进行填写'));
                return Promise.resolve(val);
              },
            },
          ]}
        />
        <ProFormSelect
          name="type"
          label="包类型"
          width="md"
          required
          initialValue={PackageTypeEnum.POS_CHARGE_APK}
          options={[
            {
              label: '收费平台POS端APP',
              value: PackageTypeEnum.POS_CHARGE_APK,
            },
          ]}
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormSelect
          name="status"
          label="包状态"
          width="md"
          required
          tooltip="当前包的开放状态, 仅当包状态为已开放时, 当前包允许请求下载"
          initialValue={PackageStatusEnum.WAIT}
          options={[
            {
              label: '待开放',
              value: PackageStatusEnum.WAIT,
            },
            {
              label: '已开放',
              value: PackageStatusEnum.OPEN,
            },
            {
              label: '已下架',
              value: PackageStatusEnum.DEPRECATED,
            },
          ]}
        />
        <ProFormSwitch
          label="是否为最新版本"
          name="isLast"
          required
          tooltip="是否将当前版本置为对应包的最新版本"
        />
      </ProForm.Group>
      <ProFormTextArea label="版本描述" name="desc" width="xl" placeholder="请输入当前版本说明" />
      <ProFormText
        name="url"
        label="下载地址"
        disabled
        rules={[
          {
            required: true,
            validator: (_, val) => {
              if (!val || !urlReg.test(val)) return Promise.reject(new Error('无效的地址'));
              return Promise.resolve(val);
            },
          },
        ]}
        width="xl"
        placeholder="请输入文件下载地址"
      />
    </ModalForm>
  );
};

export default UpdatePkg;
